/**
 * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com).
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springblade.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springblade.common.constant.MatchingConstant;
import org.springblade.core.tool.api.R;
import org.springblade.dto.ProtocolAnalysisDTO;
import org.springblade.entity.Product;
import org.springblade.entity.ProtocolAnalysis;
import org.springblade.feign.IDeviceConnectClient;
import org.springblade.service.IProductService;
import org.springblade.vo.ProtocolAnalysisVO;
import org.springblade.mapper.ProtocolAnalysisMapper;
import org.springblade.service.IProtocolAnalysisService;
import org.springblade.wrapper.ProtocolAnalysisWrapper;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;

import javax.annotation.Resource;
import java.util.List;

/**
 * 服务实现类
 * @author Blade
 * @since 2023-04-12
 */
@Service
public class ProtocolAnalysisServiceImpl extends ServiceImpl<ProtocolAnalysisMapper, ProtocolAnalysis> implements IProtocolAnalysisService {

	@Autowired
	private IProductService productService;

	@Autowired
	private IDeviceConnectClient deviceConnectClient;

	@Override
	public IPage<ProtocolAnalysisVO> selectProtocolAnalysisPage(IPage<ProtocolAnalysisVO> page, ProtocolAnalysisVO protocolAnalysis) {
		return page.setRecords(baseMapper.selectProtocolAnalysisPage(page, protocolAnalysis));
	}

	/**
	 * 指令发送
	 * @param ids
	 * @return
	 */
	@Override
	public R<Boolean> sendCode(String ids) {
		List<ProtocolAnalysis> protocolAnalyses = this.list(Wrappers.<ProtocolAnalysis>lambdaQuery().in(ProtocolAnalysis::getId, ids.split(",")));
		for (ProtocolAnalysis protocolAnalysis : protocolAnalyses) {
			Product product = productService.getById(protocolAnalysis.getProductId());
			deviceConnectClient.sendCode(product.getIpAddr(), product.getPort(), product.getProtocol(), JSONObject.toJSONString(protocolAnalysis));
		}
		return R.status(true);
	}

	@Override
	public Boolean saveOrUpdateAndFile(ProtocolAnalysisVO protocolAnalysis) {
		// todo

		Integer matchingMode = protocolAnalysis.getMatchingMode();
		if (matchingMode == MatchingConstant.GET_MATCHING || matchingMode == MatchingConstant.POST_MATCHING) {
			protocolAnalysis.setDirection(MatchingConstant.DIRECTION_ALL);
		}

		return this.saveOrUpdate(protocolAnalysis);
	}

	/**
	 * 根据产品id/功能id 查询控制列表
	 * @param protocolAnalysisDTO
	 * @return
	 */
	@Override
	public List<ProtocolAnalysisVO> getControlList(ProtocolAnalysisDTO protocolAnalysisDTO) {
		Long productId = protocolAnalysisDTO.getProductId();
		Long codeId = protocolAnalysisDTO.getId();

		List<ProtocolAnalysis> list = this.list(Wrappers.<ProtocolAnalysis>lambdaQuery()
			.eq(ObjectUtils.isNotEmpty(productId), ProtocolAnalysis::getProductId, productId)
			.eq(ObjectUtils.isNotEmpty(codeId), ProtocolAnalysis::getId, codeId));
		List<ProtocolAnalysisVO> resultList = ProtocolAnalysisWrapper.build().listVO(list);
		return resultList;
	}

	/**
	 * 获取协议列表
	 * @param address
	 * @param port
	 * @return
	 */
	@Override
	public List<ProtocolAnalysisVO> getProtocolAnalysisList(String address, String port, Integer direction) {
		return baseMapper.getProtocolAnalysisList(address, port, direction);
	}
}
